package ar.com.ford.it.fullEconomics.interfaces.strategys;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.interfaces.interfaz.InterfaceProccesFile;
import ar.com.ford.it.fullEconomics.logic.UsageLogic;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas012VehicleVw;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas104SfiVw;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesp07Usage;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesx03Intflog;
import ar.com.ford.it.fullEconomics.struct.CounterStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class UsageStrategy implements InterfaceProccesFile {
    private static int i;

    public void processLine(Object line, String userId, Collection data) {
    	
    	String aPlantCodeC = " ";
    	String aPartPrefixCodeC = " ";
    	String aPartBaseCodeC = " ";
    	String aPartSuffixCodeC = " ";
    	String aUsageDateInS = " ";
    	String aUsageDateOutS = " ";
    	String aVehicleCodeC = " ";
    	String aSfiCodeC = " ";
    	String aUsageQuantityQ = " ";
    	
    	String aLine = (String) line;
    	
    	i = i + 1;
        
    	// Carga de Planta
        aPlantCodeC = aLine.trim().toString().substring(0, 5);
        
        // Datos SFI
        aVehicleCodeC = aLine.trim().toString().substring(100,103).trim();
        aSfiCodeC = aLine.trim().toString().substring(75, 80).trim();
      
        // Datos de Usage
  
        aPartPrefixCodeC = aLine.trim().toString().substring(6, 12).trim();
        if (aPartPrefixCodeC == "") aPartPrefixCodeC = " ";
                
        aPartBaseCodeC = aLine.trim().toString().substring(12, 21).trim();
        if (aPartBaseCodeC == "") aPartBaseCodeC = " ";
        
        aPartSuffixCodeC = aLine.trim().toString().substring(21, 29).trim();
        if (aPartSuffixCodeC == "") aPartSuffixCodeC = " ";
        
        aUsageDateInS = aLine.trim().toString().substring(80, 90).trim().replace(".", "/");
        aUsageDateOutS = aLine.trim().toString().substring(90, 100).trim().replace(".", "/");
        
        aUsageQuantityQ = aLine.trim().toString().substring(32, 42).trim();
        
        System.out.println(aPlantCodeC+";"+aPartPrefixCodeC+";"+aPartBaseCodeC+";"+aPartSuffixCodeC+";"+aUsageQuantityQ+";"+aSfiCodeC+";"+aUsageDateInS+";"+aUsageDateOutS+";"+aVehicleCodeC);
    	
        // Carga de Planta
        Teesg01Plant plant = new Teesg01Plant();
        plant.setEesg01PlantCodeC(aPlantCodeC);

        // Datos SFI
        Ncas104SfiVw sfi = new Ncas104SfiVw();
        Ncas012VehicleVw vehicle = new Ncas012VehicleVw();
        vehicle.setCas012VehicleCodeC(aVehicleCodeC);
        sfi.setCas104VehicleCodeC(vehicle);
        sfi.setCas104SfiCodeC(aSfiCodeC);      
        
        Teesp07Usage usage = new Teesp07Usage();
        
        usage.setEesp07PlantCodeC(plant);
        
        usage.setEesp07PartClassCodeC("F");
        usage.setEesp07PartPrefixCodeC(aPartPrefixCodeC);
        usage.setEesp07PartBaseCodeC(aPartBaseCodeC);
        usage.setEesp07PartSuffixCodeC(aPartSuffixCodeC);
        
        usage.setNcas104SfiVw(sfi);
        
        usage.setEesp07UsageInDateY(new Timestamp(FullEconomicsDateUtil.format(
        		aUsageDateInS).getTimeInMillis()));
        usage.setEesp07UsageOutDateY(new Timestamp(FullEconomicsDateUtil
                .format(aUsageDateOutS).getTimeInMillis()));
        
        usage.setEesp07UsageQuantityQ(new BigDecimal(this
                        .convertNumber(aUsageQuantityQ)).setScale(5,BigDecimal.ROUND_HALF_EVEN));
        
        data.add(usage);        
        
    	
 /*
        String aLine = (String) line;
        Teesp07Usage usage = new Teesp07Usage();
        i = i + 1;
        // Carga de Planta
        Teesg01Plant plant = new Teesg01Plant();
        plant.setEesg01PlantCodeC(aLine.trim().toString().substring(0, 5));
        usage.setEesp07PlantCodeC(plant);

        // Datos SFI
        Ncas104SfiVw sfi = new Ncas104SfiVw();
        Ncas012VehicleVw vehicle = new Ncas012VehicleVw();
        vehicle.setCas012VehicleCodeC(aLine.trim().toString().substring(100,
                103).trim());
        sfi.setCas104VehicleCodeC(vehicle);
        sfi.setCas104SfiCodeC(aLine.trim().toString().substring(75, 80).trim());

        // Suprimido por LSPARACI
        // Carga de Pieza
        //Teesp01Part part = new Teesp01Part();
        //part.setEesp01PartPrefixCodeC(aLine.trim().toString().substring(6, 12)
        //        .trim());
        //part.setEesp01PartBaseCodeC(aLine.trim().toString().substring(12, 21)
        //        .trim());
        //part.setEesp01PartSuffixCodeC(aLine.trim().toString().substring(21, 29)
        //        .trim());
        
        
        // Datos de Usage
        // Modified by LSPARACI
        //usage.setTeesp01Part(part);
        usage.setEesp07PartClassCodeC("F");
        
        String pprefix = aLine.trim().toString().substring(6, 12).trim();
        if (pprefix == "") pprefix = " ";
        usage.setEesp07PartPrefixCodeC(pprefix);
        
        String pbase = aLine.trim().toString().substring(12, 21).trim();
        if (pbase == "") pbase = " ";
        usage.setEesp07PartBaseCodeC(pbase);
        
        String psuffix = aLine.trim().toString().substring(21, 29).trim();
        if (psuffix == "") psuffix = " ";
        usage.setEesp07PartBaseCodeC(psuffix);
        
        // end LSPARACI
        
        
        usage.setNcas104SfiVw(sfi);
        String fechaIn = aLine.trim().toString().substring(80, 90).trim()
                .replace(".", "/");
        String fechaOut = aLine.trim().toString().substring(90, 100).trim()
                .replace(".", "/");
        usage.setEesp07UsageInDateY(new Timestamp(FullEconomicsDateUtil.format(
                fechaIn).getTimeInMillis()));
        usage.setEesp07UsageOutDateY(new Timestamp(FullEconomicsDateUtil
                .format(fechaOut).getTimeInMillis()));
        String numero = aLine.trim().toString().substring(32, 42).trim();
        usage
                .setEesp07UsageQuantityQ(new BigDecimal(this
                        .convertNumber(numero)).setScale(5,BigDecimal.ROUND_HALF_EVEN));
        data.add(usage);
*/
    }

    private Double convertNumber(String datoConv) {

        Double result = new Double(0);
        String signo = null;
        String ultimoDecimal = null;
        String ultimoSigno = datoConv.trim().toString().substring(9, 10).trim();

        if ("{".equals(ultimoSigno)) {
            signo = "-";
            ultimoDecimal = "0";
        } else if ("}".equals(ultimoSigno)) {
            signo = "+";
            ultimoDecimal = "0";
        } else if ("A".equals(ultimoSigno)) {
            ultimoDecimal = "1";
            signo = "-";
        } else if ("B".equals(ultimoSigno)) {
            ultimoDecimal = "2";
            signo = "-";
        } else if ("C".equals(ultimoSigno)) {
            ultimoDecimal = "3";
            signo = "-";
        } else if ("D".equals(ultimoSigno)) {
            ultimoDecimal = "4";
            signo = "-";
        } else if ("E".equals(ultimoSigno)) {
            ultimoDecimal = "5";
            signo = "-";
        } else if ("F".equals(ultimoSigno)) {
            ultimoDecimal = "6";
            signo = "-";
        } else if ("G".equals(ultimoSigno)) {
            ultimoDecimal = "7";
            signo = "-";
        } else if ("H".equals(ultimoSigno)) {
            ultimoDecimal = "8";
            signo = "-";
        } else if ("I".equals(ultimoSigno)) {
            ultimoDecimal = "9";
            signo = "-";
        }else if ("J".equals(ultimoSigno)) {
            ultimoDecimal = "1";
            signo = "+";
        }else if ("K".equals(ultimoSigno)) {
            ultimoDecimal = "2";
            signo = "+";
        }else if ("L".equals(ultimoSigno)) {
            ultimoDecimal = "3";
            signo = "+";
        }else if ("M".equals(ultimoSigno)) {
            ultimoDecimal = "4";
            signo = "+";
        }else if ("N".equals(ultimoSigno)) {
            ultimoDecimal = "5";
            signo = "+";
        }else if ("O".equals(ultimoSigno)) {
            ultimoDecimal = "6";
            signo = "+";
        }else if ("P".equals(ultimoSigno)) {
            ultimoDecimal = "7";
            signo = "+";
        }else if ("R".equals(ultimoSigno)) {
            ultimoDecimal = "8";
            signo = "+";
        }else if ("S".equals(ultimoSigno)) {
            ultimoDecimal = "9";
            signo = "+";
        }
        if (signo == null)
            System.out.println(signo + datoConv + ultimoDecimal);
        result = Double.valueOf(signo + datoConv.toString().substring(0, 5)
                + "." + datoConv.toString().substring(5, 9) + ultimoDecimal);
        return result;
    }

    public boolean validateHeaderAndTrailer(Object aline) {
        String line = (String) aline;
        boolean conti = true;
        if (line.trim().length() < 90) {
            conti = false;
            // Lo que tengo aca es el header verlo que hago con el tambien lo
            // podria mandar a en el log ya qeu son datos necesarios
        }

        return conti;
    }

    public String getHeaderDescription(Object aLine) {
        String line = (String) aLine;
        String id = line.toString().substring(0, 7);
        String plant = line.toString().substring(7, 12);
        String file = line.toString().substring(12, 20);
        String createDate = line.toString().substring(20, 28);
        String createTime = line.toString().substring(28, 34);
        return "Id : " + id + " Codigo de planta: " + plant
                + " Nombre de archivo: " + file + " Fecha de Creacion: "
                + createDate + " Hora de creacion: " + createTime;
    }

    public String getTrailerDescription(Object aLine) {
        String line = (String) aLine;
        String id = line.toString().substring(0, 7);
        String plant = line.toString().substring(7, 12);
        String file = line.toString().substring(12, 20);
        String createDate = line.toString().substring(20, 28);
        String createTime = line.toString().substring(28, 34);
        String recCount = line.toString().substring(34, 41);
        String sumatoriaCampo = line.toString().substring(41, 51);
        return "Id : " + id + " Codigo de planta: " + plant
                + " Nombre de archivo: " + file + " Fecha de Creacion: "
                + createDate + " Hora de creacion: " + createTime
                + "Cantidad de registros :" + recCount + "   " + sumatoriaCampo;
    }

    public Integer getRecordCount(Object aLine) {
        String line = (String) aLine;
        Integer recCount = new Integer(line.toString().substring(34, 41));
        return recCount;
    }

    public CounterStruct insertData(Collection data, String userId,
                           Teesx03Intflog logNumber)
            throws FullEconomicsException {
        UsageLogic logic = new UsageLogic();
        CounterStruct counter = logic
                .createOrUpdate((Collection<Teesp07Usage>) data, userId,
                        logNumber);
        return counter;
    }

    public boolean validateRecordCount(int trailerRecordCount, int lineCount,
                                       int dataSize) {
        if (trailerRecordCount == dataSize)
            return true;
        return false;
    }
}
